<?php

namespace App\Http\Controllers\Service;


use App\Http\Controllers\Controller;

//引入外部自定义的类
use App\Models\M3Email;
use Illuminate\Http\Request;
use App\Models\M3Result;
use App\Entity\Member;
use App\Entity\TempPhone;
use App\Entity\TempEmail;
use App\Tool\UUID;
use Illuminate\Support\Facades\Mail;


class MemberController extends CommonController
{
    //用户注册的控制器
    public function register(Request $request)
    {
        //接收传递过来的数据
        $email = $request->input('email', '');
        $phone = $request->input('phone', '');
        $password = $request->input('password', '');
        $confirm = $request->input('confirm', '');
        $phone_code = $request->input('phone_code', '');
        $validate_code = $request->input('validate_code', '');

        //用于接口格式的数据返回
        $m3_result = new M3Result();

        //服务端的数据输入的合法性校验
        if ($email == '' && $phone == '') {
            $m3_result->status = 1;
            $m3_result->message = '手机号或邮箱不能为空';
            return $m3_result->toJson();
        }
        if ($password == '' || strlen($password) < 6) {
            $m3_result->status = 2;
            $m3_result->message = '密码不少于6位';
            return $m3_result->toJson();
        }
        if ($confirm == '' || strlen($confirm) < 6) {
            $m3_result->status = 3;
            $m3_result->message = '确认密码不少于6位';
            return $m3_result->toJson();
        }
        if ($password != $confirm) {
            $m3_result->status = 4;
            $m3_result->message = '两次密码不相同';
            return $m3_result->toJson();
        }

        // 手机号注册
        if ($phone != '') {
            if ($phone_code == '' || strlen($phone_code) != 6) {
                $m3_result->status = 5;
                $m3_result->message = '手机验证码为6位';
                return $m3_result->toJson();
            }

            //通过手机号在temp_phone表中查询一条数据
            $tempPhone = TempPhone::where('phone', $phone)->first();
            if ($tempPhone->code == $phone_code) {
                if (time() > strtotime($tempPhone->deadline)) {//过了验证码的有效期，将时间字符串转换为时间格式
                    $m3_result->status = 7;
                    $m3_result->message = '手机验证码不正确';
                    return $m3_result->toJson();
                }

                $isExist=Member::where('phone',$phone)->first();
                if($isExist!=null){
                    $m3_result->status = 10;
                    $m3_result->message = '该手机号已被注册';
                    return $m3_result->toJson();
                }

                //数据入库
                $member = new Member();
                $member->phone = $phone;
                $member->password = md5('bk' + $password);
                $member->save();
                //返回注册成功的信息
                $m3_result->status = 0;
                $m3_result->message = '注册成功';
                return $m3_result->toJson();
            } else {
                $m3_result->status = 7;
                $m3_result->message = '手机验证码不正确';
                return $m3_result->toJson();
            }

            // 邮箱注册
        } else {
            if ($validate_code == '' || strlen($validate_code) != 4) {
                $m3_result->status = 6;
                $m3_result->message = '验证码为4位';
                return $m3_result->toJson();
            }

            $validate_code_session = $request->session()->get('validate_code', '');
            if ($validate_code_session != $validate_code) {//如果session里面的验证码和客户端传递过来的验证码不一致的时候
                $m3_result->status = 8;
                $m3_result->message = '验证码不正确';
                return $m3_result->toJson();
            }

            //查询用户输入的邮箱是否已经存在
            $isExist = Member::where('email', $email)->first();
            if ($isExist != null) {
                $m3_result->status = 9;
                $m3_result->message = '该邮箱已被注册';
                return $m3_result->toJson();
            }

            //将用户信息存入数据库
            $member = new Member();
            $member->email = $email;
            $member->password = md5('bk' + $password);
            $member->save();

            $uuid = UUID::create(); //调用指定外部类的静态方法
            $m3_email = new M3Email();
            $m3_email->to = $email; //邮件的目的邮箱
            $m3_email->cc = 'lb1562481877@sina.com'; //邮件的抄送地址
            $m3_email->subject = '斌哥商城验证'; //邮件的主题
            $m3_email->content = '请于24小时点击该链接完成验证. http://web.bookshop.com/service/validate_email'
                . '?member_id=' . $member->id
                . '&code=' . $uuid;  //邮件的内容

            $tempEmail = new TempEmail();
            $tempEmail->member_id = $member->id;
            $tempEmail->code = $uuid;
            $tempEmail->deadline = date('Y-m-d H-i-s', time() + 24 * 60 * 60); //有效时间为1天
            $tempEmail->save();

            Mail::send('email_register', ['m3_email' => $m3_email], function ($m) use ($m3_email) {
                // $m->from('hello@app.com', 'Your Application');
                $m->to($m3_email->to, '尊敬的用户')//发送给谁
                ->cc($m3_email->cc)//抄送
                ->subject($m3_email->subject);//主题
            });

            $m3_result->status = 0;
            $m3_result->message = '注册成功';
            return $m3_result->toJson();
        }
    }

    public function login(Request $request)
    {
        $username = $request->get('username', '');
        $password = $request->get('password', '');
        $validate_code = $request->get('validate_code', '');
        $m3_result = new M3Result();

        //数据合法性校验
        if ($username == null) {
            $m3_result->status = 5;
            $m3_result->message = '用户名不能为空';
            return $m3_result->toJson();
        }
        if ($password == null) {
            $m3_result->status = 6;
            $m3_result->message = '密码不能为空';
            return $m3_result->toJson();
        }
        if ($validate_code == null) {
            $m3_result->status = 7;
            $m3_result->message = '验证码不能为空';
            return $m3_result->toJson();
        }
        if (strlen($validate_code) != 4) {
            $m3_result->status = 8;
            $m3_result->message = '验证码只能为4位';
            return $m3_result->toJson();
        }

        //判断
        $validate_code_session = $request->session()->get('validate_code', '');
        if ($validate_code_session != $validate_code) {
            $m3_result->status = 1;
            $m3_result->message = '验证码不正确';
            return $m3_result->toJson();
        }
        if (strpos($username, '@') == true) { //如果username里面包含@字符
            $member = Member::where('email', $username)->first();
        } else {
            $member = Member::where('phone', $username)->first();
        }

        if ($member == null) {
            $m3_result->status = 2;
            $m3_result->message = '该用户不存在';
            return $m3_result->toJson();
        } else {
            if (md5('bk' + $password) != $member->password) { //将客户端传递过来的密码进行加密操作后再与数据库中的字段进行比对
                $m3_result->status = 3;
                $m3_result->message = '密码不正确';
                return $m3_result->toJson();
            }
        }

        $request->session()->put('member', $member);//用户登录成功，就将该用户的信息存入session里面
        $m3_result->status = 0;
        $m3_result->message = '登录成功';
        return $m3_result->toJson();
    }
}
